package com.getpebble.android.main.sections.mypebble.view;

import android.content.Context;
import android.database.DataSetObserver;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.getpebble.android.R;

public class IconPageIndicator extends LinearLayout implements OnPageChangeListener {
    private PageDataSetObserver mDataObserver;
    private ViewPager mViewPager;

    private class PageDataSetObserver extends DataSetObserver {
        private PageDataSetObserver() {
        }

        public void onChanged() {
            super.onChanged();
            IconPageIndicator.this.notifyDataSetChanged();
        }
    }

    public IconPageIndicator(Context context) {
        super(context);
        init(context);
    }

    public IconPageIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public IconPageIndicator(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        this.mDataObserver = new PageDataSetObserver();
    }

    public void setViewPager(ViewPager viewPager) {
        if (this.mViewPager != null) {
            this.mViewPager.setOnPageChangeListener(null);
            this.mViewPager.getAdapter().unregisterDataSetObserver(this.mDataObserver);
        }
        this.mViewPager = viewPager;
        this.mViewPager.setOnPageChangeListener(this);
        this.mViewPager.getAdapter().registerDataSetObserver(this.mDataObserver);
        notifyDataSetChanged();
    }

    public void notifyDataSetChanged() {
        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        int numIcons = getChildCount();
        int numPages = this.mViewPager.getAdapter().getCount();
        if (numIcons < numPages) {
            int numToAdd = numPages - numIcons;
            for (int i = 0; i < numToAdd; i++) {
                addView((ImageView) layoutInflater.inflate(R.layout.page_indicator, null));
            }
        } else if (numIcons > numPages) {
            for (int remove = numIcons - numPages; remove < numIcons; remove++) {
                View viewToRemove = getChildAt(remove);
                if (viewToRemove != null) {
                    removeView(viewToRemove);
                }
            }
        }
        setCurrentItem(this.mViewPager.getCurrentItem());
    }

    private void setCurrentItem(int index) {
        int i = 0;
        while (i < getChildCount()) {
            getChildAt(i).setSelected(i == index);
            i++;
        }
    }

    public void onPageScrolled(int i, float v, int i2) {
    }

    public void onPageSelected(int i) {
        setCurrentItem(i);
    }

    public void onPageScrollStateChanged(int i) {
    }
}
